package com.xomily.devops.service.impl;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xomily.common.utils.DateUtils;
import com.xomily.common.utils.sign.Md5Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xomily.devops.mapper.DevNetLogMapper;
import com.xomily.devops.mapper.DevNetMapper;
import com.xomily.devops.domain.DevAlarmLog;
import com.xomily.devops.domain.DevNet;
import com.xomily.devops.domain.DevNetLog;
import com.xomily.devops.service.IDevAlarmLogService;
import com.xomily.devops.service.IDevNetLogService;
import com.xomily.system.service.ISysConfigService;

/**
 * 网络日志Service业务层处理
 * 
 * @author xomily
 * @date 2021-11-29
 */
@Service
public class DevNetLogServiceImpl implements IDevNetLogService 
{
    @Autowired
    private DevNetLogMapper devNetLogMapper;
    @Autowired
    private DevNetMapper devNetMapper;
    @Autowired
    private IDevAlarmLogService alarmLogService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询网络日志
     * 
     * @param id 网络日志主键
     * @return 网络日志
     */
    @Override
    public DevNetLog selectDevNetLogById(Long id)
    {
        return devNetLogMapper.selectDevNetLogById(id);
    }

    /**
     * 查询网络日志列表
     * 
     * @param devNetLog 网络日志
     * @return 网络日志
     */
    @Override
    public List<DevNetLog> selectDevNetLogList(DevNetLog devNetLog)
    {
        return devNetLogMapper.selectDevNetLogList(devNetLog);
    }

    /**
     * 查询网络运维日志列表
     * 
     * @param Map 条件集合
     * @return 网络运维日志集合
     */

    public Map<String, Object> selectDevNetLogByGraph(Map<String, Object> params) {
		double[] rxbytData = new double[240];
		double[] txbytData = new double[240];		
		String[] xAxis = new String[240];
		List<DevNetLog> logList = devNetLogMapper.selectDevNetLogByGraph(params);
		int h,m,n;
		
		for(int i=0; i<240; i++){
			h = (i/10);
			m = (i%10*6);
			xAxis[i]= String.format("%02d",h)+":"+String.format("%02d",m); 
		} 
		double rxbyt = 0;
		double txbyt = 0;
		
		for (DevNetLog log : logList) {
            Date createTime = log.getCreateTime();
            h = DateUtils.getHour(createTime);
            m = DateUtils.getMinute(createTime); 
            n = (h*60 + m)/6;	
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            rxbyt = Double.valueOf(decimalFormat.format((float) log.getRxbyt() / (float) (128*1024)));
            txbyt = Double.valueOf(decimalFormat.format((float) log.getTxbyt() / (float) (128*1024)));

            if (rxbyt>rxbytData[n]) {
	 			rxbytData[n]= rxbyt;				
			}
			if (txbyt>txbytData[n]) {
				txbytData[n]= txbyt;				
			}

		}		
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rxbytGraph",rxbytData);
		ret.put("txbytGraph",txbytData);		
        ret.put("xAxis",xAxis);
		return ret;
	}
    /**
     * 新增网络日志
     * 
     * @param devNetLog 网络日志
     * @return 结果
     */
    @Override
    public int insertDevNetLog(DevNetLog devNetLog)
    {
    	DevNet netQuery = new DevNet();
        netQuery.setIp(devNetLog.getIp());
        netQuery.setName(devNetLog.getName());
        DevNet net=devNetMapper.selectDevNetByIpName(netQuery);
        if (net != null) {
        	net.setRxbyt(devNetLog.getRxbyt());
        	net.setTxbyt(devNetLog.getTxbyt());
        	net.setRxpck(devNetLog.getRxpck());
        	net.setTxpck(devNetLog.getTxpck());
        	net.setRxerr(devNetLog.getRxerr());
        	net.setTxerr(devNetLog.getTxerr());  
            net.setUpdateTime(DateUtils.getNowDate());
            devNetLog.setNetId(net.getId());
        	devNetMapper.updateDevNet(net);
        }
	    
        double txbps = Double.parseDouble(configService.selectConfigByKey("dev.host.txbps"));
        if ((txbps>0) && (devNetLog.getTxbyt()>txbps*128)) {
        	DevAlarmLog alarmTxLog = new DevAlarmLog();
            String content = "网络发送速率大于告警值"+ configService.selectConfigByKey("dev.host.txbps")+"kbps";
            alarmTxLog.setType("NetWarn");
            alarmTxLog.setIp(devNetLog.getIp());
            alarmTxLog.setCreateTime(DateUtils.getNowDate());
            alarmTxLog.setGenerateTime(devNetLog.getGenerateTime());
            alarmTxLog.setContent(content);
            alarmTxLog.setCode( Md5Utils.hash(alarmTxLog.getType()+alarmTxLog.getContent()));
            alarmLogService.insertDevAlarmLog(alarmTxLog);
        }
        
        
	    double rxbps = Double.parseDouble(configService.selectConfigByKey("dev.host.rxbps"));
        if ((rxbps>0) && (devNetLog.getRxbyt()>rxbps*128)) {
        	DevAlarmLog alarmRxLog = new DevAlarmLog();
            String content = "网络接收速率大于告警值"+ configService.selectConfigByKey("dev.host.rxbps")+"kbps";
            alarmRxLog.setType("NetWarn");
            alarmRxLog.setIp(devNetLog.getIp());
            alarmRxLog.setCreateTime(DateUtils.getNowDate());
            alarmRxLog.setGenerateTime(devNetLog.getGenerateTime());
            alarmRxLog.setContent(content);
            alarmRxLog.setCode( Md5Utils.hash(alarmRxLog.getType()+alarmRxLog.getContent()));
            alarmLogService.insertDevAlarmLog(alarmRxLog);
        }
        
        devNetLog.setCreateTime(DateUtils.getNowDate());
        return devNetLogMapper.insertDevNetLog(devNetLog);
    }

    /**
     * 修改网络日志
     * 
     * @param devNetLog 网络日志
     * @return 结果
     */
    @Override
    public int updateDevNetLog(DevNetLog devNetLog)
    {
        return devNetLogMapper.updateDevNetLog(devNetLog);
    }

    /**
     * 批量删除网络日志
     * 
     * @param ids 需要删除的网络日志主键
     * @return 结果
     */
    @Override
    public int deleteDevNetLogByIds(Long[] ids)
    {
        return devNetLogMapper.deleteDevNetLogByIds(ids);
    }

    /**
     * 删除网络日志信息
     * 
     * @param id 网络日志主键
     * @return 结果
     */
    @Override
    public int deleteDevNetLogById(Long id)
    {
        return devNetLogMapper.deleteDevNetLogById(id);
    }
}
